package com.tsing.basic.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.basic.service.IRoleService;
import com.tsing.basic.vo.req.*;
import com.tsing.basic.vo.resp.PermissionRespVo;
import com.tsing.basic.vo.resp.PermissionTreeRespVo;
import com.tsing.basic.vo.resp.RoleRespVo;
import com.tsing.common.annotations.SystemLog;
import com.tsing.common.utils.ResultUtil;
import com.tsing.common.vo.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/addRole")
    @ApiOperation(value = "新增角色")
    @SystemLog(description = "新增角色")
    public ResultData addRole(@Validated @RequestBody RoleAddReqVo reqVo) {
        roleService.addRole(reqVo);
        return ResultUtil.success();
    }

    @DeleteMapping("/deleteRole")
    @ApiOperation(value = "删除角色")
    @SystemLog(description = "删除角色")
    public ResultData deleteRole(@Validated @RequestBody IdsReqVo reqVo) {
        roleService.deleteRole(reqVo);
        return ResultUtil.success();
    }

    @GetMapping("/getRole")
    @ApiOperation(value = "获取单个角色信息")
    @SystemLog(description = "获取单个角色信息")
    public ResultData<RoleRespVo> getRoleById(@Validated IdReqVo reqVo) {
        return ResultUtil.success(roleService.getRoleById(reqVo));
    }

    @PutMapping("/updateRole")
    @ApiOperation(value = "修改角色信息")
    @SystemLog(description = "修改角色信息")
    public ResultData updateRole(@Validated @RequestBody RoleUpdateReqVo reqVo) {
        roleService.updateRole(reqVo);
        return ResultUtil.success();
    }

    @GetMapping("/rolePage")
    @ApiOperation(value = "角色分页")
    @SystemLog(description = "角色分页")
    public ResultData<Page<RoleRespVo>> rolePage(@Validated RolePageReqVo reqVo) {
        return ResultUtil.success(roleService.rolePage(reqVo));
    }

    @GetMapping("/getPermission")
    @ApiOperation(value = "获取单个角色权限")
    @SystemLog(description = "获取单个角色权限")
    public ResultData<List<PermissionRespVo>> getPermissionByRoleId(@Validated IdReqVo reqVo) {
        return ResultUtil.success(roleService.getPermissionByRoleId(reqVo));
    }

    @GetMapping("/getPermissionTree")
    @ApiOperation(value = "获取权限树")
    @SystemLog(description = "获取权限树")
    public ResultData<List<PermissionTreeRespVo>> getPermissionTree() {
        return ResultUtil.success(roleService.getPermissionTree());
    }

    @PostMapping("/bindPermission")
    @ApiOperation(value = "角色绑定权限")
    @SystemLog(description = "角色绑定权限")
    public ResultData bindPermission(@Validated @RequestBody BindPermissionReqVo reqVo) {
        roleService.bindPermission(reqVo);
        return ResultUtil.success();
    }

}
